<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Loader;
use think\Session;

class Index extends Controller
{
    public function index()
    {
		// if(isset($_COOKIE["uid"])){
		// 	//勾选记住密码
		// 	setcookie("uid",$_COOKIE["uid"]);
		// 	setcookie("realname",$_COOKIE["realname"]);
		// 	setcookie("uname",$_COOKIE["uname"]);
		// 	setcookie("rid",$_COOKIE["rid"]);
		// 	switch ($_COOKIE["rid"]) {
        //         case 1:$this->redirect('net/index');
        //         //网络保修
        //         case 2:$this->redirect('room/index');
        //         //物业报修
        //         case 3:$this->redirect('suggest/index');
        //         //投诉建议
        //         default:$this->redirect('user/userlist');
		// }
		if(isset($_COOKIE['uid'])){
			Session::set("uid",$_COOKIE['uid']);
			Session::set("realname",$_COOKIE['realname']);
			Session::set("uname",$_COOKIE['uname']);
			Session::set("rid",$_COOKIE['rid']);

			switch ($_COOKIE["rid"]) {
				case 1:
					$this->redirect('net/index');
				case 2:
					$this->redirect('room/index');
				case 3:
					$this->redirect('user/userlist');	
				
				default:$this->success('登录成功','user/userlist');
			}
		}

        return $this->fetch();
    }
    //处理登录
    public function login(){
    	//判断账号密码是否正确，验证表单数据是否正确输入
    	//判断账户是否被禁用
    	//根据用户角色进入不同的管理界面
		


    	$data=input('post.');
    	//dump($data);
    	
    	$validate=\think\Loader::validate('User');

    	if(!$validate->check($data)){
    		$this->error($validate->getError());
    	}

    	$data=[
    		'uname'=>input('uname'),
    		'pw'=>md5(input('pw'))
    	];

    	$res=Db::name('user')->where($data)->find();
    	//dump($res);
    	if (!$res) {
    		$this->error("账号密码错误");
    	}elseif($res['status']==1){
    		$this->error("账号已经被禁用");
    	}else{
			
			if(isset($_POST["rm"])){   //勾选了记住密码
				setcookie("uid",$res['uid'],time()+(3600*24*30));
				setcookie("realname",$res['realname'],time()+(3600*24*30));
				setcookie("uname",$res['uname'],time()+(3600*24*30));
				setcookie("rid",$res['rid'],time()+(3600*24*30));

			}

    		Session::set("uid",$res["uid"]);
    		Session::set("realname",$res["realname"]);
    		Session::set("uname",$res["uname"]);
    		Session::set("rid",$res["rid"]);

    		// $res["rid"]=4;

    		switch ($res["rid"]) {
                case 1:$this->success('登录成功','net/index');
                //网络保修
                case 2:$this->success('登录成功','room/index');
                //物业报修
                case 3:$this->success('登录成功','suggest/index');
                //投诉建议
                default:$this->success('登录成功','user/userlist');
                //rid1网络部
                //2物业部
                //3学工部
    		}
    	}
    }
    //注销
    public function logout(){
		setcookie("uid","",time()-1);
		setcookie("realname","",time()-1);
		setcookie("uname","",time()-1);
		setcookie("rid","",time()-1);

    	Session::clear();
    	$this->redirect("index/index");
    }
}
